/**
 * @(#)CourseFilter.java
 *
 * Copyright: Copyright (c) 2003 Carnegie Mellon University
 *
 * This program is the implementation of the original system in the Assignment 1 of "Architectures
 * of Software Systems" class. For the details of each classes, please refer to the provided javadoc
 * documents. You will also find some useful information such as implementation decisions, coding
 * standards, and naming conventions in the README file.
 *
 * History:
 *
 * Date          Author              Change
 * ----------    ----------------    ---------------------------------------------------------------
 * 07/15/2003    Jung Soo Kim        Initial version.
 */


import java.io.*;


/**
 * A course filter has one input port and one output port. It reads data from the input port and
 * pass them to the output port if a certain condition meets. Here, data are passed if a student
 * has completed a given course.
 *
 * @author Jung Soo Kim
 * @version 1.0
 */
public class CourseFilter extends Filter {

    /**
     * The input port.
     **/
    protected BufferedReader pInput;

    /**
     * The output port.
     **/
    protected BufferedWriter pOutput;

    /**
     * A course number for screening
     **/
    protected int iCourse;

    /**
     * Construct a course filter with the given name. A course filter requires an input port and
     * an output port. Ports are expected to be wrapped into a buffered character stream. This
     * approach enables using various different kinds of I/O mechanisms as well as pipes.
     * For example, a file can be directly plugged into a filter.
     *
     * @param sName   the string representing this filter's name
     * @param pInput  the input port of this filter
     * @param pOutput the output port of this filter
     * @param iCourse the course number for screening
     */
    public CourseFilter(String sName, 
                        BufferedReader pInput, BufferedWriter pOutput, int iCourse) {
        // Execute parent's constructor first.
        super(sName);

        // Initialize input/output ports.
        this.pInput  = pInput;
        this.pOutput = pOutput;

        // Set the course number for screening.
        this.iCourse = iCourse;
    }

    /**
     * Tells if data are available on the input port.
     *
     * @return <code>true</code> if and only if this filter can read data from the input port.
     * @throws IOException
     */
    protected boolean ready() throws IOException {
        return this.pInput.ready();
    }

    /**
     * Read available data from the input port and write them to the output port if the student
     * represented by the read data (a student record) has completed the given course passed when
     * creating this course filter.
     *
     * @throws IOException
     */
    protected void work() throws IOException {
        // Read a student record from the input port.
        Student objStudent = new Student(this.pInput.readLine());

        // Write the student record to the output port if it passes the screening.
        if (objStudent.hasCompleted(this.iCourse)) {
            this.pOutput.write(objStudent.toString());
            this.pOutput.newLine();
            this.pOutput.flush();
        }
    }
}
